package com.slz3.business.module.goodsManagement.gooAttributeValue.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.goodsManagement.gooAttributeValue.domain.GooAttributeValue;
import com.slz3.business.module.goodsManagement.gooAttributeValue.domain.GooAttributeValueFactory;
import com.slz3.business.module.goodsManagement.gooAttributeValue.steward.GooAttributeValueSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Slf4j
@Service
public class GooAttributeValueServiceImpl implements GooAttributeValueService {
    @Override
    public GooAttributeValue save(GooAttributeValue gooAttributeValue) {
        return Repositorys.gooAttributeValue.save(GooAttributeValueFactory.initBaseFields(gooAttributeValue));
    }

    @Override
    public Page<GooAttributeValue> page(MultiValueMap<String, String> params) {
        Page<GooAttributeValue> gooAttributeValuePage = Repositorys.gooAttributeValue.findAll(
                GooAttributeValueSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return gooAttributeValuePage;
    }


    @Override
    public GooAttributeValue modify(GooAttributeValue gooAttributeValueNew) {
        return Repositorys.gooAttributeValue.saveAndFlush(GooAttributeValueFactory.initBaseFields(gooAttributeValueNew));
    }

    @Override
    public Optional<GooAttributeValue> single(UUID uuid) {
        return Repositorys.gooAttributeValue.findById(uuid);
    }

    @Override
    public List<GooAttributeValue> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.gooAttributeValue.findAllById(uuids);
    }

    @Override
    public List<GooAttributeValue> delete(List<GooAttributeValue> gooAttributeValueList) {
        gooAttributeValueList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            GooAttributeValueFactory.initBaseFields(item);
        });
        return Services.gooAttributeValueTransactional.batchModify(gooAttributeValueList);
    }

}
